<?php

namespace Home\Controller;

use Think\Controller;

class VipController extends CommonController
{
    //会员中心
    public function index(){
        $user_info = M('user_info');
        $res = $user_info -> field('exp,vipday') -> where('user_id='.$_SESSION['home']['uid']) -> select();
        $this -> assign('exp',$res[0]['exp']);
        $this -> assign('grade',floor( sqrt( $res[0]['exp'] ) ) );
        $this -> assign('test','会员中心');
        //计算会员剩余事件
        $vipday = ceil( ( $res[0]['vipday'] - time() ) / 24 / 3600 );
        if ($vipday <=0) {
            $vipday = 0;
        }
        $this -> assign('vipday',$vipday.'天');
        $this -> display();
    }

    //ajax  返回生成的二维码
    public function ewm(){
        if(IS_AJAX){
        //当用户输入非法数字时
        if ($_POST['buynum'] <1 || $_POST['buynum'] >24) {
            echo 'no';die;
        }
        //得到用户要购买的时限
        $buynum = $_POST['buynum'];
        // $buynum = 1;

        //生成验证用的vip键值对
        $vip['vip_key'] = $_POST['rnum'];
        $vip['user_id'] = $_SESSION['home']['uid'];
        $vip['vip_value'] = md5( mt_rand() );
        // 将验证键值对写入数据库
        $vipdb = M('vip');
        $vipdb -> add($vip);
        $array['vipvalue'] = $vip['vip_value'];
        //拼接请求URL
        $url = "http://192.168.45.32/zhong/index.php/Home/Api/payvip/buynum/".$buynum."/id/".$_SESSION['home']['uid']."/vipkey/".$vip['vip_key']."/vipvalue/".$vip['vip_value'];
        // $res = file_get_contents( 'http://qr.liantu.com/api.php?text='.$url );
        $array['img'] = '<img src="http://qr.liantu.com/api.php?text='.$url.'" style="width:30%">';
       $this -> ajaxReturn($array);
        }
    }

    //轮讯用ajax
    public function gangRape(){
        //得到传过来的随机数
        $data['vip_key'] = $_POST['rnum'];
        $data['user_id'] = $_SESSION['home']['uid'];
        // var_dump($data);die;
        $vip = M('vip');
        $res = $vip -> where($data) -> select();
        if ($res[0]['status'] == '1') {
            echo 'yes';
            //删除数据库中的验证信息
            $vip -> where($data) ->delete();
        }else{
            echo 'no';
        }
    }

    //等级兑换页面
    public function show(){
        //得到banner的信息
        $banner = M('banner');
        $res = $banner -> field('banner_path') -> where('status = 0') -> select();
        $array = array_column($res, 'banner_path');
        $this -> assign('banner',$array);
        //得到会员特权介绍的内容
        $path = "./Public/Uploads/Banner/vipcontents.txt";
        $contents = file_get_contents($path);
        $this -> assign('contents',$contents);
        $this -> assign('test','会员特权');
        $this -> display();
    }

}
